<?php
/*
*	@todo	实现电子合同签章过程
*/
require_once dirname(dirname(__FILE__)) . '/include/common.inc.php';
require_once dirname(dirname(__FILE__)) . '/include/econtract.class.php';
require_once INCLUDE_ROOT_PATH . 'file_server.class.php';


$type = $data['type'] = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';	//用户id
$filename = $data['filename'] = isset($_REQUEST['filename']) ? $_REQUEST['filename'] : '';	//操作，view在线查看，download下载
$cust_id = $data['cust_id'] = isset($_REQUEST['cust_id']) ? $_REQUEST['cust_id'] : '';	//操作，view在线查看，download下载
$act = $data['act'] = isset($_REQUEST['act']) ? $_REQUEST['act'] : '';	//操作，view在线查看pdf，在线查看pdf的图片格式，download下载pdf
$name = $data['name'] =  isset($_REQUEST['name']) ? $_REQUEST['name'] : '';	//要查看或下载的文件名
$idcard = $data['idcard'] =  isset($_REQUEST['idcard']) ? $_REQUEST['idcard'] : '';	//要查看或下载的文件名
$mobile = $data['mobile'] =  isset($_REQUEST['mobile']) ? $_REQUEST['mobile'] : '';	//要查看或下载的文件名
$accountId = $data['accountId'] = isset($_REQUEST['accountId']) ? $_REQUEST['accountId'] : '';	//要查看或下载的文件名
$color = $data['color'] = isset($_REQUEST['color']) ? $_REQUEST['color'] : '';	//要查看或下载的文件名
$hText = $data['hText'] = isset($_REQUEST['hText']) ? $_REQUEST['hText'] : '';	//要查看或下载的文件名
$qText = $data['qText'] = isset($_REQUEST['qText']) ? $_REQUEST['qText'] : '';	//要查看或下载的文件名
$sealData = isset($_REQUEST['sealData']) ? $_REQUEST['sealData'] : '';	//要查看或下载的文件名
$data['sealData'] = urlencode($sealData);
$sealData = urldecode($sealData);
$srcPdfFile = $data['srcPdfFile'] = isset($_REQUEST['srcPdfFile']) ? $_REQUEST['srcPdfFile'] : '';	//要查看或下载的文件名
$dstPdfFile = $data['dstPdfFile'] = isset($_REQUEST['dstPdfFile']) ? $_REQUEST['dstPdfFile'] : '';	//要查看或下载的文件名
$signType = $data['signType'] = isset($_REQUEST['signType']) ? $_REQUEST['signType'] : '';	//要查看或下载的文件名
$posPage = $data['posPage'] = isset($_REQUEST['posPage']) ? $_REQUEST['posPage'] : '';	//要查看或下载的文件名
$posX = $data['posX'] = isset($_REQUEST['posX']) ? $_REQUEST['posX'] : '';	//要查看或下载的文件名
$posY = $data['posY'] = isset($_REQUEST['posY']) ? $_REQUEST['posY'] : '';	//要查看或下载的文件名
$key = $data['key'] = isset($_REQUEST['key']) ? $_REQUEST['key'] : '';	//要查看或下载的文件名
$once = $data['once'] = isset($_REQUEST['once']) ? $_REQUEST['once'] : '';	//时间戳，用来判断有效期
//$token = isset($_REQUEST['token']) ? $_REQUEST['token'] : '';	//token验证
$personarea = $data['personarea'] = isset($_REQUEST['personarea']) ? $_REQUEST['personarea'] : '';	//法人或者客户所在区域
$usertype = $data['usertype'] = isset($_REQUEST['usertype']) ? $_REQUEST['usertype'] : '';	//注册类型，1-代理人注册，2-法人注册，默认1（可空）
$agentname = $data['agentname'] = isset($_REQUEST['agentname']) ? $_REQUEST['agentname'] : '';	//代理人姓名，当注册类型为1时必填
$agentIdNo = $data['agentidno'] = isset($_REQUEST['agentidno']) ? $_REQUEST['agentidno'] : '';	//代理人身份证号，当注册类型为1时必填
$legalName = $data['legalname'] = isset($_REQUEST['legalname']) ? $_REQUEST['legalname'] : '';	//代理人姓名，当注册类型为1时必填
$legalIdNo = $data['legalidno'] = isset($_REQUEST['legalidno']) ? $_REQUEST['legalidno'] : '';	//代理人身份证号，当注册类型为1时必填

if (!check_signature_str($data))
{
	if ($act == 'view') {
		header("Content-type: text/html; charset=utf-8");
		exit('验签失败！');
	} else {
		exit(json_encode(array('code' => 0, 'msg' => '验签失败！')));
	}
}
else
{
	if (empty($personarea)) {
		$personarea = '0';
	}
	if (empty($usertype)) {
		$usertype = '1';
	}
    if(!file_exists(PDF_ROOT_PATH) && !is_dir(PDF_ROOT_PATH)){
		mkdir(PDF_ROOT_PATH); 
	}  
	$file_dir = PDF_ROOT_PATH . $cust_id . '/';
    $srcPdf = $file_dir . $srcPdfFile;// 原始文件
    $first = $file_dir . $dstPdfFile;//第一次签章后文件
	$file_dir = PDF_ROOT_PATH . $cust_id . '/';
	$filepath = $file_dir . $filename;
	$File_server = new File_server();
    $econtract = new Econtract();
	switch ($act)
	{
		case 'addAccount':
            $res = $econtract->addAccount($type, $mobile, $name, $idcard, $personarea, $usertype, $agentname,$agentIdNo,$legalIdNo, $legalName);
            if($res['errCode'] == 0){
                $result = $res['accountId'];
            }else{
                $result = json_encode($res);
            }
            echo $result;
			break;
        case 'addTemplateSeal':
			$res = $econtract->addTemplateSeal($type, $accountId, $color, $hText, $qText);
            if($res['errCode'] == 0){
                $result = $res['sealData'];
            }else{
                $result = $res;
            }
            echo json_encode($result);
			break;
		case 'userSign':
			$res = $econtract->userSign($accountId, $sealData, $srcPdf, $first, $fileName, $signType, $posPage, $posX, $posY, $key);
			if($res['errCode'] == 0){
                $result = 1;
            }else{
                $result = $res;
            }
            echo json_encode($result);
			break;            

		case 'download':
			$result = $File_server->download($filepath);
			break;
		case 'pdfimgpath':
			$result = $File_server->pdf2jpg($filepath, $file_dir . "images/", $filename);
			echo json_encode($result);
			exit;
			break;
		case 'viewimg':
			$imagepath = $file_dir . "images/" . $filename;
			$result = $File_server->view($imagepath);
            echo json_encode($result);exit;
			break;
		default:
			$result = false;
			break;
	}
}

function check_signature_str($data = array())
{
	$commonstr = "econtractview";
	$signature = isset($_REQUEST['token']) ? $_REQUEST['token'] : '';
	$string = md5(implode(array(strtoupper($data['act']), $commonstr)));
	if(!empty($data) && is_array($data))
	{
		ksort($data);
		foreach($data as $key => $value) 
		{
			$string .= urldecode($value);
		}
		return md5($string) == $signature ? true : false;
	}
	else
	{
		return false;
	}
}

exit;
?>